import random
import os
class Cal():

    def cala(self, number, choose):
        if choose == 'number':
            try:
                number = int(number)
            except:
                number = 0
            dataset = '1234567890'
            res = ''.join([random.choice(dataset) for i in range(number)])
        elif choose == 'symbol':
            try:
                number = int(number)
            except:
                number = 0
            dataset = '~!@#$%^&*()_+{}|:<>?/.,;[]\=-`'
            res = ''.join([random.choice(dataset) for i in range(number)])
        elif choose == 'letter':
            try:
                number = int(number)
            except:
                number = 0
            dataset = 'qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP'
            res = ''.join([random.choice(dataset) for i in range(number)])
        elif choose == 'chinese':
            try:
                number = int(number)
            except:
                number = 0
            filepath = os.path.join(os.getcwd(), r'static/txt/hanzi.txt')
            f = open(filepath, 'r')
            dataset = f.read().split()
            res = ''.join([random.choice(dataset) for i in range(number)])
            # res = ''.join([chr(random.randint(0x4E00, 0x9FBF)) for i in range(number)])
        else:
            try:
                number = int(number)
            except:
                number = 0
            dataset = '1234567890~!@#$%^&*()_+{}|:<>?/.,;[]\=' \
                      '-`qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP'
            res = ''.join([random.choice(dataset) for i in range(number)])
        return res

    def cradno(self):
        filepath = os.path.join(os.getcwd(),'static/txt/citycode.txt')
        f = open(filepath, 'r')
        noaddress = f.read().split('\n')
        choose_no_address = ''.join([random.choice(noaddress)])
        address_res = choose_no_address.split()
        one_to_six = address_res[0]
        address = address_res[1:len(address_res)]
        # 生成出生日期
        year = list(range(1900, 2017))
        month = list(range(1, 13))
        day1 = list(range(1, 29))
        day2 = list(range(1, 30))
        day3 = list(range(1, 31))
        day4 = list(range(1, 32))
        res = ''.join([str(random.choice(year))])
        if int(res)%4 == 0 or int(res)%100 == 0 or int(res)%400 == 0:
            res1 = ''.join([str(random.choice(month))])
            # 填充0
            res1 = res1.rjust(2, '0')
            if int(res1) == 2:
                res2 = ''.join([str(random.choice(day1))])
                res2 = res2.rjust(2, '0')
            elif int(res1)%2 == 0:
                res2 = ''.join([str(random.choice(day3))])
                res2 = res2.rjust(2, '0')
            else:
                res2 = ''.join([str(random.choice(day4))])
                res2 = res2.rjust(2, '0')
        else:
            res1 = ''.join([str(random.choice(month))])
            res1 = res1.rjust(2, '0')
            if int(res1) == 2:
                res2 = ''.join([str(random.choice(day2))])
                res2 = res2.rjust(2, '0')
            elif int(res1) % 2 == 0:
                res2 = ''.join([str(random.choice(day3))])
                res2 = res2.rjust(2, '0')
            else:
                res2 = ''.join([str(random.choice(day4))])
                res2.rjust(2, '0')
        seven_to_fourteen = res + res1 + res2
        fifteen_to_sixteen = str(random.randint(1,99)).rjust(2, '0')
        seventeen = str(random.randint(1, 2))
        card = one_to_six + seven_to_fourteen + fifteen_to_sixteen + seventeen
        # 计算校验码
        t = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        temp_res = 0
        for i in range(len(t)-1):
            temp_res += int(card[i]) * t[i]
        the_map = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
        eighteen = the_map[temp_res%11]
        card_no_res = card + str(eighteen)
        return card_no_res